<template>
  <div class="app-container">
    <div class="p4">
      <FaSearchBar :show-toggle="false">
        <template #default="{ fold, toggle }">
          <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="80px">
            <el-form-item label="模板编码" prop="code">
              <el-input v-model="queryParams.code" placeholder="请输入模板编码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="模板名称" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入模板名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="类型" prop="type" v-show="!fold">
              <el-select v-model="queryParams.type" placeholder="请输入类型" clearable style="width: 150px"
                @change="getList">
                <el-option v-for="dict in sys_template_type" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="级别" prop="level" v-show="!fold">
              <el-select v-model="queryParams.level" placeholder="请输入级别" clearable style="width: 150px"
                @change="getList">
                <el-option v-for="dict in sys_template_level" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="status" v-show="!fold">
              <el-select v-model="queryParams.status" placeholder="请输入状态" clearable style="width: 150px"
                @change="getList">
                <el-option v-for="dict in sys_common_status" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button @click="resetQuery">重置</el-button>
              <el-button link @click="toggle">
                <template #icon>
                  <FaIcon :name="fold ? 'i-ep:caret-bottom' : 'i-ep:caret-top'" />
                </template>
                {{ fold ? '高级查询' : '高级查询' }}
              </el-button>
            </el-form-item>
          </el-form>
        </template>
      </FaSearchBar>
      <el-divider border-style="dashed" style="margin:0px 0px 20px 0px" />

      <el-space wrap>
        <el-button type="primary" icon="Plus" @click="handleAdd" v-auth="['system:template$add']">新增</el-button>
        <el-button type="success" icon="Edit" :disabled="single" @click="handleUpdate"
          v-auth="['system:template$edit']">修改</el-button>
        <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete"
          v-auth="['system:template$remove']">删除</el-button>
        <el-button type="warning" icon="Download" @click="handleExport"
          v-auth="['system:template$export']">导出</el-button>

      </el-space>

      <el-table v-loading="loading" :data="templateList" @selection-change="handleSelectionChange" stripe
        highlight-current-row border class="mt4">
        <el-table-column type="selection" width="55" align="center" />
        <!--      <el-table-column label="编号" align="center" prop="id" />-->
        <el-table-column label="模板编码" align="center" prop="code" width="200" />
        <el-table-column label="模板名称" align="left" prop="name" />
        <!--      <el-table-column label="模板内容" align="center" prop="content" />-->
        <el-table-column label="模板类型" align="center" prop="type" width="200">
          <template #default="scope">
            <DictTag :dictData="sys_template_type" :value="scope.row.type" />
          </template>
        </el-table-column>
        <el-table-column label="级别" align="center" prop="level" width="100">
          <template #default="scope">
            <DictTag :dictData="sys_template_level" :value="scope.row.level" />
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status" width="100">
          <template #default="scope">
            <DictTag :dictData="sys_common_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button size="small" plain type="success" icon="Edit" @click="handleUpdate(scope.row)"
              v-auth="['system:template$edit']">修改</el-button>
            <el-button size="small" plain type="danger" icon="Delete" @click="handleDelete(scope.row)"
              v-auth="['system:template$remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </div>
    <!-- 添加或修改模板信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="templateRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" clearable placeholder="请输入类型">
            <el-option v-for="dict in sys_template_type" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="模板编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入模板编码" />
        </el-form-item>
        <el-form-item label="模板名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入模板名称" />
        </el-form-item>
        <el-form-item label="模板内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入模板内容" />
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-select v-model="form.level" clearable placeholder="请输入级别">
            <el-option v-for="dict in sys_template_level" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item>
          <template #label>
            <span>
              <el-tooltip content="选择停用则不能被使用" placement="top">
                <el-icon><question-filled /></el-icon>
              </el-tooltip>
              状态
            </span>
          </template>
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in sys_common_status" :key="dict.value" :label="dict.value">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Template">
import apiTemplate from "@/api/system/template";

const { proxy } = getCurrentInstance();

const { sys_common_status, sys_template_type, sys_template_level } = proxy.useDict("sys_common_status", "sys_template_type", "sys_template_level");

const templateList = ref([]);
const open = ref(false);
const loading = ref(false);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    code: null,
    name: null,
    type: null,
    level: null,
    status: null,
  },
  rules: {
    code: [
      { required: true, message: "模板编码不允许为空", trigger: "blur" }
    ],
    name: [
      { required: true, message: "模板名称不允许为空", trigger: "blur" }
    ],
    content: [
      { required: true, message: "模板内容不允许为空", trigger: "blur" }
    ],
    type: [
      { required: true, message: "模板类型不允许为空", trigger: "change" }
    ],
    level: [
      { required: true, message: "级别不允许为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态不允许为空", trigger: "change" }
    ],
    delFlag: [
      { required: true, message: "删除标志不允许为空", trigger: "blur" }
    ],
    createdBy: [
      { required: true, message: "创建人不允许为空", trigger: "blur" }
    ],
    createdAt: [
      { required: true, message: "创建时间不允许为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询模板信息列表 */
function getList() {
  // loading.value = true;
  apiTemplate.page(queryParams.value).then(response => {
    templateList.value = response.data.rows;
    total.value = response.data.total;
  }).finally(() => {
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    code: null,
    name: null,
    content: null,
    type: null,
    level: null,
    status: '1',
    remark: null
  };
  proxy.resetForm("templateRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加模板信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  apiTemplate.getById(_id).then(response => {
    form.value = response.data;
    form.value.type = response.data.type + "";
    form.value.level = response.data.level + "";
    form.value.status = response.data.status + "";
    open.value = true;
    title.value = "修改模板信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["templateRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        apiTemplate.update(form.value).then(response => {
          proxy.$modal.success("修改成功");
          open.value = false;
          getList();
        });
      } else {
        apiTemplate.add(form.value).then(response => {
          proxy.$modal.success("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除模板信息编号为"' + _ids + '"的数据项？').then(function () {
    return apiTemplate.delete(_ids);
  }).then(() => {
    getList();
    proxy.$modal.success("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  apiTemplate.download(queryParams.value, "模板列表");
}

getList();
</script>
